# # -*- coding: utf-8 -*-
#
# import os
# from os.path import join
# import re
# import subprocess
# import time
# from string import Template
#
# currpath = os.path.dirname(os.path.realpath(__file__))
#
# # jmeter路径
# JMETER_Home = r'''"D:\Program Files\apache-jmeter-5.5\bin\jmeter.bat"'''
#
# # jmeter模板
# origin_jmxfile = replaced_jmxfile = ''
#
#
# def before_check():
#     origin_jmxfile
#     count_jmxfile = 0
#     for root, dirs, files in os.walk(currpath):
#         for name in files:
#             if '-P.jmx' in name:
#                 os.remove(join(root, name))
#             if '-P.jmx' not in name and '.jmx' in name and 'XL' not in name:
#                 count_jmxfile = count_jmxfile + 1
#                 origin_jmxfile = join(root, name)
#     if count_jmxfile != 1 and origin_jmxfile:
#         print('为了更智能地执行jmx文件，请确保有且仅有一个有效的jmx文件!')
#         return False
#     else:
#         return True
#
#
# def create_para_jmxfile():
#     origin_jmxfile, replaced_jmxfile
#     jmx_str = ''
#     with open(origin_jmxfile, "r", encoding="utf-8") as file:
#         jmx_str = file.read()
#     patten = '<stringProp name="LoopController.loops">(.*?)</stringProp>'
#     replace_str = '<stringProp name="LoopController.loops">$loops</stringProp>'
#     jmx_str = re.sub(patten, replace_str, jmx_str)
#     patten = '<stringProp name="ThreadGroup.num_threads">(.*?)</stringProp>'
#     replace_str = '<stringProp name="ThreadGroup.num_threads">$num_threads</stringProp>'
#     jmx_str = re.sub(patten, replace_str, jmx_str)
#     replaced_jmxfile = origin_jmxfile.replace('.jmx', '-P.jmx')
#     with open(replaced_jmxfile, "w+", encoding="utf-8") as file:
#         file.writelines(jmx_str)
#
#
# def getDateTime():
#     '''
#     获取当前日期时间，格式'20150708085159'
#     '''
#     return time.strftime(r'%Y%m%d%H%M%S', time.localtime(time.time()))
#
#
# def execcmd(command):
#     print(f"command={command}")
#
#     output = subprocess.Popen(
#         command,
#         stdout=subprocess.PIPE,
#         stderr=subprocess.PIPE,
#         shell=True,
#         universal_newlines=True)
#
#     stderrinfo, stdoutinfo = output.communicate()
#     print(f"stderrinfo={stderrinfo}")
#     print(f"stdoutinfo={stdoutinfo}")
#     print(f"returncode={output.returncode}")
#
#
# def execjmxs(Num_Threads, Loops):
#     tmpstr = ''
#     with open(replaced_jmxfile, "r", encoding="utf-8") as file:
#         tmpstr = Template(file.read()).safe_substitute(
#             num_threads=Num_Threads, loops=Loops)
#     now = getDateTime()
#     tmpjmxfile = currpath + f'\{now}-T{Num_Threads}XL{Loops}.jmx'
#     with open(tmpjmxfile, "w+", encoding="utf-8") as file:![](/uploads/photo/2018/66ee114e-365a-4742-825c-f6f68a49926d.png!large)
#
#     file.writelines(tmpstr)
# csvfilename = currpath + f"\{now}-result.csv"
# htmlreportpath = currpath + f"\{now}-htmlreport"
# if not os.path.exists(htmlreportpath):
#     os.makedirs(htmlreportpath)
# execjmxouthtml = f"cmd.exe /c {JMETER_Home} -n -t {tmpjmxfile} -l {csvfilename} -e -o {htmlreportpath}"
# execcmd(execjmxouthtml)
#
#
# if before_check():
#     create_para_jmxfile()
#     jobs = [dict(Num_Threads=(x+1), Loops=1) for x in range(10)]
#     [execjmxs(x["Num_Threads"], x["Loops"]) for x in jobs]